include("util.js");
include("tinyxmldom.js");

//var karotz_ip="localhost";
var karotz_ip="192.168.0.15";

var XmlData = http.get("http://weather.yahooapis.com/forecastrss?p=FRXX0076&u=c");
var objDom = new XMLDoc(XmlData);
var domTree = objDom.docNode;
var domNode = domTree.selectNode("/channel/item/yweather:forecast[0]");
 
var buttonListener = function(event) {
    if (event == "DOUBLE") {
        karotz.tts.stop();
        exit();
    }
    return true;
}

var onKarotzConnect = function(data) {
	karotz.button.addListener(buttonListener);
	karotz.tts.start("Weather Conditions for Paris", "en", lows);
}

var exitFunction = function(event) {
  if(event == "CANCELLED" || event == "TERMINATED")  {
        exit();
    }
    return true;
}

var lows = function(event) {
	if(event == "CANCELLED" || event == "TERMINATED")  {
		var todaylow = domNode.getAttribute("low");
		karotz.tts.start("Today's low is " + todaylow, "en", highs);
    }
}

var highs = function(event) {
	if(event == "CANCELLED" || event == "TERMINATED")  {
		var todayhigh = domNode.getAttribute("high");
		karotz.tts.start("Today's high is " + todayhigh, "en", exitFunction);
    }
}

karotz.connectAndStart(karotz_ip, 9123, onKarotzConnect, {});
